package com.goodmean.teacher.teacher;
import com.goodmean.common.VO.TeaGradeBean;
import com.goodmean.common.VO.TeacherGradeModel;
import com.goodmean.common.model.*;
import com.goodmean.student.StudentService;
import com.goodmean.teacher.admin.ClazzCourseService;
import com.goodmean.teacher.admin.ClazzService;
import com.goodmean.teacher.admin.CourseService;
import com.goodmean.teacher.admin.CourseStudentGradeService;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherService {

    public static final TeacherService me = new TeacherService();
    private static final Teacher teaDao = new Teacher().dao();
    private static final CourseService courseService = new CourseService();
    private static final ClazzService clazzService= new ClazzService();
    private ClazzCourseService clazzCourseService = ClazzCourseService.me;
    private CourseStudentGradeService courseStudentGradeService = CourseStudentGradeService.me;
    private StudentService studentService = StudentService.me;
	private int flag = 0;
    public List<Teacher> findAll(){

        return teaDao.find("SELECT * FROM teacher");

    }
    public Teacher findById(Integer teacherId){
        /**
         * 通过教师编号查找教师个人信息
         */
        return teaDao.findById(teacherId);
    }
    public void update(Teacher teacher ,Integer teacher_id) {

        Teacher teacher1 = teaDao.findById(teacher_id);
        teacher1.setTeacherName(teacher.getTeacherName());
        teacher1.setTeacherAddress(teacher.getTeacherAddress());
        teacher1.setTeacherPhone(teacher.getTeacherPhone());
        teacher1.setTeacherBoardDate(teacher.getTeacherBoardDate());
        //teacher1.setTeacherPassword(teacher.getTeacherPassword());
        teacher1.update();
    }

    /**
     * 添加教师信息
     * @param teacher 教师对象
     * @return boolean 是否添加成功
     */
    public boolean addTeacher(Teacher teacher) {
        boolean canAdd = teacher.save();
        return  canAdd;
    }

    /**
     * 根据教师编号删除教师信息
     * @param teacherId 教师编号
     * @return boolean 是否删除成功
     */
    public boolean deleteTeacher(Integer teacherId) {
        boolean canDelete = teaDao.deleteById(teacherId);
        return  canDelete;
    }

    /**
     * 更新教师信息
     * @param teacher 教师对象
     * @return boolean 是否更新成功
     */
    public boolean updateTeacher(Teacher teacher) {
        boolean canUpdate = teacher.update();
        return  canUpdate;
    }

    /**
     * 根据教师编号，教师名字查询教师信息
     * @param teacherId 教师信息
     * @param teacherName 教师名字
     * @return List<Teacher> 教师信息
     */
    public Page<Teacher> findTeacher(Integer teacherId, String teacherName, int page){
        Page<Teacher> paginate;
        String sqlExceptSelect = "FROM student_info.teacher WHERE 1=1" + addWhere(teacherId, teacherName);
        switch (flag) {
            case(0): paginate = teaDao.paginate(page, 10, "SELECT *", sqlExceptSelect);break;
            case(1): paginate = teaDao.paginate(page, 10, "SELECT *", sqlExceptSelect, teacherId);break;
            case(2): paginate = teaDao.paginate(page, 10, "SELECT *", sqlExceptSelect, teacherName);break;
            default: paginate = teaDao.paginate(page, 10, "SELECT *", sqlExceptSelect, teacherId, teacherName); break;
        }
        flag = 0;
        return paginate;
    }

    public String addWhere(Integer teacherId, String teacherName) {
        StringBuffer where = new StringBuffer(" ");
        if(StrKit.notNull(teacherId)) {
            flag += 1;
            where.append("AND teacher_id = ?");
        }
        if(StrKit.notBlank(teacherName)) {
            flag += 2;
            where.append("AND teacher_name = ? ");
        }
        return where.toString();
    }
    public Map<Integer, String> findMyCourse(String clomunName, Integer id) {
        String sql;

        if ("tearcher_id".equals(clomunName)) {
            sql = "SELECT * FROM clazz_course " + "WHERE 1=1 AND " + clomunName + " = " + id;
        } else {
            sql = "SELECT * FROM clazz_course  " + "WHERE 1=1 AND " + clomunName + " = '" + id + "'";
        }
        Map<Integer,String> courses = setNull();
        List<ClazzCourse> list = clazzCourseService.findByTeacherId(id);
        for (ClazzCourse tsc : list) {
            String cn = courseService.findById(tsc.getCourseId()).getCourseName();
            String tn = teaDao.findById(tsc.getTeacherId()).getTeacherName();
            String cp = tsc.getCoursePlace();
            Integer clazz_id = tsc.getClazzId();
            String className = clazzService.findById( clazz_id).getClazzName();

            String courseMessage = cn + "&nbsp;（" + tn + "）&nbsp;" + cp + "("+ className + ")";
            String time = tsc.getCourseTime();

            //将表clazz_course中的列course_time转换成二维表格式
            for (String s : time.split(",")) {
                String[] cour = s.split("-");
                Integer num = Integer.valueOf(cour[1] + cour[0]);
                courses.put(num, courseMessage);
            }

        }

        return courses;
    }

    /* 将二维表中与key对应的value设为空字符串，防止#(Map.key)报错 */
    private Map<Integer, String> setNull () {
        Map<Integer, String> list = new HashMap<>();
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 6; j++) {
                int n = i * 10 + j;
                list.put(n, "");
            }
        }
        return list;
    }
    public void findStudentScore(Integer courseId){

        courseService.findById(courseId);


    }
    public void findScoreByCourseId(Integer courseId){

        courseStudentGradeService.findByCourseId(courseId);

    }
    /** 查找班级所有学生和他的成绩 **/
    public TeacherGradeModel findAllStuGrades(Integer clazzId , Integer courseId){
        Course course = courseService.findById(courseId);
        List<Student> students  = studentService.findByClazzId(clazzId);
        List<TeaGradeBean> teaGradeBeanList = new ArrayList<>();
        for(Student s : students){
           CourseStudentGrade csg = courseStudentGradeService.findByStudentIdCourseId(s.getStudentId() , courseId);
           Integer grade = null;
           if (csg != null){
               grade = csg.getCourseGrade();
           }
           teaGradeBeanList.add(new TeaGradeBean(grade , s.getStudentId() , s.getStudentName()));
        }
        TeacherGradeModel teacherGradeModel = new TeacherGradeModel(course.getCourseName() , courseId , clazzId ,teaGradeBeanList);
        return teacherGradeModel;
    }
}


